In this exercise, we will be exploring Interactive Data Visualisation with R by using the packages ggiraph, plotyr and gganimate
Before you get started, you are required:
Next, you will use the code chunk below to install and launch ggiraph, DT, plotly, tidyverse, patchwork, readxl, gifski and gapminder in RStudio.
packages = c('ggiraph', 'plotly',
'DT', 'patchwork',
'gganimate', 'tidyverse',
'readxl', 'gifski', 'gapminder')
for(p in packages){library
if(!require(p, character.only = T)){
install.packages(p)
}
library(p, character.only = T)
}
exam_data <- read_csv("data/Exam_data.csv")
An html widget and a ggplot2 extension. It allows ggplot graphics to be interactive.
Interactive is made with ggplot geometries that can understand three arguments:
If it used within a shiny application, elements associated with an id (data_id) can be selected and manipulated on client and server sides.
p <- ggplot(data=exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(tooltip = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618
)
Interactivity: Elements associated with a data_id (i.eCLASS) will be highlighted upon mouse over.
Note that the default value of the hover css is hover_css = “fill:orange;”.
p <- ggplot(data=exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = c(CLASS)),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618
)
In the code chunk below, css codes are used tochange the highlighting effect.
p <- ggplot(data=exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = CLASS),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618,
options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
)
)
Interactivity: Elements associated with a data_id (i.e CLASS) will be highlighted upon mouse over.
Interactivity: Web document link with a data object will be displayed on the web browser upon mouse click.
exam_data$onclick <- sprintf("window.open(\"%s%s\")",
"https://www.moe.gov.sg/schoolfinder?journey=Primary%20school", as.character(exam_data$ID) )
p <- ggplot(data=exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(onclick = onclick),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
scale_y_continuous(NULL,
breaks = NULL)
girafe(
ggobj = p,
width_svg = 6,
height_svg = 6*0.618)
Coordinated multiple views methods has beenimplemented in the data visualisation on the right. + when a data point of one of the dotplot isselected, the corresponding data point ID on thesecond data visualisation will be highlighted too.
In order to build a coordinated multiple views, the following programming strategy will be used:
p1 <- ggplot(data=exam_data,
aes(x = MATHS)) +
geom_dotplot_interactive(
aes(data_id = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
p2 <- ggplot(data=exam_data,
aes(x = ENGLISH)) +
geom_dotplot_interactive(
aes(data_id = ID),
stackgroups = TRUE,
binwidth = 1,
method = "histodot") +
coord_cartesian(xlim=c(0,100)) +
scale_y_continuous(NULL,
breaks = NULL)
girafe(code = print(p1 / p2),
width_svg = 6,
height_svg = 6,
options = list(
opts_hover(css = "fill: #202020;"),
opts_hover_inv(css = "opacity:0.2;")
)
)
The data_id aesthetic is critical to link observations between plots and the tooltip aesthetic is optional but nice to have when mouse over a point.
Plotly’s R graphing library create interactive web graphics from ggplot2 graphs and/or a custom interface tothe (MIT-licensed) JavaScript library plotly.js inspired by the grammar of graphics.
Different from other plotly platform, plot.R is free and open source.
There are two ways to create interactive graph by using plotly, they are: + by using plot_ly(), and + by using ggplotly()
The code chunk below plots an interactive scatter plot by using plot_ly().
plot_ly(data = exam_data,
x = ~MATHS,
y = ~ENGLISH)
In the code chunk below, color argument is mapped to a qualitative visual variable (i.e. RACE).
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
color = ~RACE)
To interact with the graph, click on the colour symbol at the legend.
In the code chunk below, colors argument is used to change the default colour palette to ColorBrewel colour palette.
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
color = ~RACE,
colors = "Set1")
To interact with the graph, click on the colour symbol at the legend.
In the code chunk below, a customised colour scheme is created. Then, colors argument is used to change the default colour palette to the customised colour scheme.
pal <- c("red", "purple", "blue", "green")
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
color = ~RACE,
colors = pal)
In the code chunk below, text argument is used to change the default tooltip.
plot_ly(data = exam_data,
x = ~ENGLISH,
y = ~MATHS,
text = ~paste("Student ID:", ID,
"<br>Class:", CLASS),
color = ~RACE,
colors = "Set1")
In the code chunk below, layout argument is used to change the default tooltip.
The code chunk below plots an interactive scatter plot by using ggplotly().
Notice that the only extra line you need to include in the code chunk is ggplotly().
Code chunk below plots two scatterplots and places them next to each other side-by-side by using subplot() of plotly package.
To create a coordinated scatterplots, highlight_key() of plotly package is used.
d <- highlight_key(exam_data)
p1 <- ggplot(data=d,
aes(x = MATHS,
y = ENGLISH)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
p2 <- ggplot(data=d,
aes(x = MATHS,
y = SCIENCE)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
subplot(ggplotly(p1),
ggplotly(p2))
Click on a data point of one of the scatterplot and see how the corresponding point on the other scatterplot is selected.
Things to learn from the code chunk:
A wrapper of the JavaScript Library DataTables
Data objects in R can be rendered as HTML tables using the JavaScript library ‘DataTables’ (typically via R Markdown or Shiny).
DT::datatable(exam_data)
d <- highlight_key(exam_data)
p <- ggplot(d,
aes(ENGLISH,
MATHS)) +
geom_point(size=1) +
coord_cartesian(xlim=c(0,100),
ylim=c(0,100))
gg <- highlight(ggplotly(p),
"plotly_selected")
crosstalk::bscols(gg,
DT::datatable(d),
widths = 5)
Things to learn from the code chunk:
highlight() is a function of plotly package. It sets a variety of options for brushing (i.e., highlighting) multiple plots. These options are primarily designed for linking multiple plotly graphs, and may not behave as expected when linking plotly to another htmlwidget package via crosstalk. In some cases, other htmlwidgets will respect these options, such as persistent selection in leaflet.
bscols() is a helper function of crosstalk package. It makes it easy to put HTML elements side by side. It can be called directly from the console but is especially designed to work in an R Markdown document. Warning: This will bring in all of Bootstrap!
gganimate extends the grammar of graphics as implemented by ggplot2 to include the description of animation. It does this by providing a range of new grammar classes that can be added to the plot object in order to customise how it should change with time.
We will be using the following packages in the packages list:
We then import the Data worksheet from GlobalPopulation Excel workbook.
globalPop <- read_xls("data/globalpopulation.xls")
We will first plot a static bubble plot
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young')
Next, we plot an animated population bubble plot to see the difference
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
#we use geom_point to control opacity
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
#we use scale_size to control the sizes of the circles
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') +
transition_time(Year) +
ease_aes('linear')